package lp2.projeto.agenda;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import lp2.projeto.horario.Horario;
import lp2.projeto.horario.Mes;

/**
 * Dialogo responsavel por questionar uma data ao usuario
 */
public class DataJDialog extends JDialog {
	private static final long serialVersionUID = -4445172635805597340L;
	
	private int dia, ano;
	private Mes mes;
	
	private JLabel perguntaJLabel;
	private JComboBox diaCombo;
	private JComboBox mesCombo;
	private JComboBox anoCombo;
	private JButton cancelarJButton;
	private JButton okJButton;
	
	/**
	 * Construtor de DataJDialog
	 * @param pergunta Pergunta a ser feita ao usuario
	 * @param acaoOk Acao caso o usuario aperte ok
	 */
	public DataJDialog(String pergunta, final ActionListener acaoOk) {
		GridBagConstraints constraints = new GridBagConstraints();
		JPanel panel = (JPanel)getContentPane();
		panel.setLayout(new GridBagLayout());
		
		perguntaJLabel = new JLabel(pergunta);
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 5;
		panel.add(perguntaJLabel, constraints);
		
		constraints.gridy = 1;
		constraints.gridwidth = 1;
		constraints.ipadx = 5;
		constraints.anchor = GridBagConstraints.EAST;
		panel.add(new JLabel("Data:"), constraints);
		
		// Criacao do JComboBox dos Dias
		Integer[] listaDias = new Integer[31];
		for (int i = 1; i <= 31; i++)	listaDias[i-1] = i;	
		diaCombo = new JComboBox(listaDias);
		
		constraints.gridx = 1;
		panel.add(diaCombo, constraints);
		
		// Criacao do JComboBox dos Meses
		mesCombo = new JComboBox(Mes.values());
		constraints.gridx = 2;
		panel.add(mesCombo, constraints);
		
		// Criacao do JComboBox dos Anos
		Integer[] listaAnos = new Integer[111];
		for (int i = 2011; i > 1900; i--)	listaAnos[2011-i] = i;	
		anoCombo = new JComboBox(listaAnos);
		
		constraints.gridx = 3;
		panel.add(anoCombo, constraints);
		
		constraints.gridy = 2;
		constraints.fill = GridBagConstraints.HORIZONTAL;

		cancelarJButton = new JButton("Cancelar");
		cancelarJButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		constraints.gridx = 2;
		panel.add(cancelarJButton, constraints);
		
		okJButton = new JButton("Ok");
		okJButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ano = (Integer)anoCombo.getSelectedItem();
				dia = (Integer)diaCombo.getSelectedItem();
				mes = (Mes)mesCombo.getSelectedItem();
				try {
					new Horario(ano, mes, dia, 0, 0);
					acaoOk.actionPerformed(arg0);
					dispose();
				} catch (IllegalArgumentException ex) {
					avisoJDialog aviso = new avisoJDialog("Data invalida");
					aviso.setVisible(true);					
				}
			}
		});
		
		constraints.gridx = 3;
		panel.add(okJButton, constraints);

		setTitle("Escolha uma data");
		
		setSize(Math.max(280, perguntaJLabel.getMinimumSize().width+10), 100);
		setModal(true);
		setResizable(false);
	}
	
	/**
	 * @return O dia escolhido pelo usuario
	 */
	public int getDia() {
		return dia;
	}
	
	/**
	 * @return O mes escolhido pelo usuario
	 */
	public Mes getMes() {
		return mes;
	}
	
	/**
	 * @return O ano escolhido pelo usuario
	 */
	public int getAno() {
		return ano;
	}
}
